package ysq.xyg.recovery.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.recovery.dao.RecoveryProposeDao;
import ysq.xyg.recovery.model.po.RecoveryPropose;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.recovery.model.vo.RecoveryProposeVo;
import ysq.xyg.recovery.service.RecoveryProposeService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 回收金模块-建议表service实现
 *
 * @author congcong tao
 * @date 2019-5-14 14:21:56
 */
@Service("recoveryProposeService")
public class RecoveryProposeServiceImpl extends BaseServiceImpl<RecoveryPropose> implements RecoveryProposeService {

    @Resource
    private RecoveryProposeDao recoveryProposeDao;

    private RecoveryPropose recoveryPropose;

    @Autowired
    public void setBaseDao(RecoveryProposeDao recoveryProposeDao) {
        this.baseDao = recoveryProposeDao;
    }

    /**
     * 分页查询
     *
     * @param recoveryProposeVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-14 14:21:56
     */
    @Override
    public ReturnEntity page(RecoveryProposeVo recoveryProposeVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<RecoveryPropose> list = acceptList(recoveryProposeVo);
        // list
        if (list != null && list.size() > 0) {
            for (RecoveryPropose recoveryPropose : list) {
                recoveryPropose.setMemberName(BaseUtil.decodeBase64(recoveryPropose.getMemberName()));
            }
        }
        PageInfo<RecoveryPropose> page=new PageInfo<RecoveryPropose>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param recoveryProposeVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-14 14:21:56
     */
    @Override
    public ReturnEntity addOrUpdate(RecoveryProposeVo recoveryProposeVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(recoveryProposeVo.getId())){
            recoveryPropose=new RecoveryPropose(currentUser);
            isUpate=false;
        }else{
            recoveryPropose=selectByPrimaryKey(recoveryProposeVo.getId());
            if(ObjectUtil.isNull(recoveryPropose)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            recoveryPropose.setUpdateTime(new Date());
            recoveryPropose.setUpdateUser(currentUser.getId());
        }
        recoveryPropose.setMemberId(recoveryProposeVo.getMemberId());
        recoveryPropose.setMemberName(recoveryProposeVo.getMemberName());
        recoveryPropose.setContent(recoveryProposeVo.getContent());
        recoveryPropose.setStatus(recoveryProposeVo.getStatus());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKeySelective(recoveryPropose);
        }else{
            num=insert(recoveryPropose);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-14 14:21:56
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        RecoveryPropose recoveryPropose=selectByPrimaryKey(id);
        if(recoveryPropose!=null){
            recoveryPropose.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            recoveryPropose.setUpdateTime(new Date());
            recoveryPropose.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(recoveryPropose);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param recoveryProposeVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-5-14 14:21:56
     */
    @Override
    public List<RecoveryPropose> acceptList(RecoveryProposeVo recoveryProposeVo){
        Example example = new Example(RecoveryPropose.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(recoveryProposeVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",recoveryProposeVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(recoveryProposeVo.getMemberName())){
            String memberName = BaseUtil.encodeBase64String(recoveryProposeVo.getMemberName());
            criteria.andLike("memberName", "%" + memberName + "%");
        }
        example.setOrderByClause(" update_time desc");
        return recoveryProposeDao.selectByExample(example);
    }
}
